<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.png">
  <link rel="mask-icon" href="/images/avatar.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"notes.maxwi.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="首先需要清楚数组名是数组首元素的地址，不管是一维还是多维，它是个常量，而指针是个可以存储地址的变量。使用时可以将数组名看成一个常量指针。　　C&#x2F;C++中没有内置的多维数组，多维数组是数组的数组。例如二维数组就是元素为一维数组的一维数组，此时数组名依然是数组的第一个元素的地址。具体解释看代码注释及代码的运行结果。　　C&#x2F;C++中的函数无法返回数组，所以在使用函数操作数组时，只能通过指针或者结构体">
<meta property="og:type" content="article">
<meta property="og:title" content="举例详解一维数组、二维数组与指针、函数的问题">
<meta property="og:url" content="http://notes.maxwi.com/2016/03/15/array-pointer-function/index.html">
<meta property="og:site_name" content="blueyi&#39;s notes">
<meta property="og:description" content="首先需要清楚数组名是数组首元素的地址，不管是一维还是多维，它是个常量，而指针是个可以存储地址的变量。使用时可以将数组名看成一个常量指针。　　C&#x2F;C++中没有内置的多维数组，多维数组是数组的数组。例如二维数组就是元素为一维数组的一维数组，此时数组名依然是数组的第一个元素的地址。具体解释看代码注释及代码的运行结果。　　C&#x2F;C++中的函数无法返回数组，所以在使用函数操作数组时，只能通过指针或者结构体">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2016-03-15T08:57:38.000Z">
<meta property="article:modified_time" content="2016-03-15T08:57:38.000Z">
<meta property="article:author" content="blueyi">
<meta property="article:tag" content="CPP">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://notes.maxwi.com/2016/03/15/array-pointer-function/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>举例详解一维数组、二维数组与指针、函数的问题 | blueyi's notes</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="blueyi's notes" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="blueyi's notes" type="application/rss+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">blueyi's notes</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Follow Excellence,Success will chase you!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://notes.maxwi.com/2016/03/15/array-pointer-function/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/default_avatar.jpg">
      <meta itemprop="name" content="blueyi">
      <meta itemprop="description" content="心怀善意，虛怀若谷！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="blueyi's notes">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          举例详解一维数组、二维数组与指针、函数的问题
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2016-03-15 16:57:38" itemprop="dateCreated datePublished" datetime="2016-03-15T16:57:38+08:00">2016-03-15</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C++</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="Symbols count in article">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">Symbols count in article: </span>
              <span>6.4k</span>
            </span>
            <span class="post-meta-item" title="Reading time">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">Reading time &asymp;</span>
              <span>6 mins.</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>　　首先需要清楚数组名是数组首元素的地址，不管是一维还是多维，它是个常量，而指针是个可以存储地址的变量。使用时可以将数组名看成一个常量指针。<br>　　C/C++中没有内置的多维数组，多维数组是数组的数组。例如二维数组就是元素为一维数组的一维数组，此时数组名依然是数组的第一个元素的地址。<br>具体解释看代码注释及代码的运行结果。<br>　　C/C++中的函数无法返回数组，所以在使用函数操作数组时，只能通过指针或者结构体等方式来间接返回数组，当然可以返回数组名，因为数组是个地址，相当于一个常量指针。具体详解可以看SOF: <a target="_blank" rel="noopener" href="http://stackoverflow.com/questions/8617889/return-a-2d-array-from-a-function">Return a 2d array from a function</a>  </p>
<span id="more"></span>

<p>以下代码为了方便查看和分析，所以只是代码片段  </p>
<h1 id="一维数组与指针"><a href="#一维数组与指针" class="headerlink" title="一维数组与指针"></a>一维数组与指针</h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> ar[<span class="number">20</span>] = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;;  <span class="comment">//前3个元素分别是1，2，3</span></span><br><span class="line"><span class="type">int</span> *p1 = ar; <span class="comment">//将p1指向ar数组的第一个元素的地址，指向的是数组的元素，p1+1的增量为1个元素所占的字节空间</span></span><br><span class="line"><span class="built_in">int</span> (*p2)[<span class="number">20</span>] = &amp;ar; <span class="comment">//将p2指向ar数组的地址，即指向整个数组，p2+1的增量为&amp;ar所代表的数组所占的字节空间</span></span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;p1+1: &quot;</span> &lt;&lt; p1+<span class="number">1</span> &lt;&lt; <span class="string">&quot;\tar+1: &quot;</span> &lt;&lt; ar+<span class="number">1</span> &lt;&lt; <span class="string">&quot;\t&amp;ar[1]: &quot;</span> &lt;&lt; &amp;ar[<span class="number">1</span>] &lt;&lt; std::endl;</span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;*(p1+1): &quot;</span> &lt;&lt; *(p1+<span class="number">1</span>) &lt;&lt; <span class="string">&quot;\t*(ar+1): &quot;</span> &lt;&lt; *(ar+<span class="number">1</span>) &lt;&lt; std::endl;</span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;*p2: &quot;</span> &lt;&lt; *p2 &lt;&lt; <span class="string">&quot;\tp1: &quot;</span> &lt;&lt; p1 &lt;&lt; <span class="string">&quot;\t**p2: &quot;</span> &lt;&lt; **p2 &lt;&lt; <span class="string">&quot;\t*p1: &quot;</span> &lt;&lt; *p1 &lt;&lt; std::endl;</span><br></pre></td></tr></table></figure>
<p>p1是一个指向int型变量的指针，p1存储的地址与ar是一样的，都是数组第一个元素的地址，但p1是指针变量，ar是个常量地址，p1++等价于p1=p1+1， 但不能使用ar++。下标的操作在C++中会转化为对地址的操作，如ar[i]会被转化为<em>(ar + i), p2是一个指针，不同于int *p2[20];不加小括号时，p2会优先与后面的[20]结合，导致p2成为指针数组, 即含有20个int型指针的数组。而加了小括号p2先与</em>结合，p2成为指针，该指针指向一个含有20个int 型元素的数组，恰好与ar数组类型相同。注意ar是一个数组，数组名表示的是该数组的第一个元素的地址。 例外情况是对数组名使用sizeof运算符将获得整个数组的大小（单位是字节）。 而对数组名取地址时，得到的将是该连续20个地址空间的首地址，虽然值相同，但意义不同。 &amp;ar[0]/p1/ar表示的是一个4字节的内存块的首地址，而&amp;ar却表示20<em>4个字节的内存块首地址。 相当于此时对p1/ar加1时增加的最小单位是4个字节的连续内存空间，也就是对应第二个元素，而对&amp;ar/p2加1是毫无意义的，增加是20</em>4个字节的连续内存空间，将会越界， 可对其解引用一次得到数组第一个元素的地址，即*p2等价于p1。  </p>
<h1 id="二维数组与指针"><a href="#二维数组与指针" class="headerlink" title="二维数组与指针"></a>二维数组与指针</h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">std::cout &lt;&lt; <span class="string">&quot;**********二维数组与指针*********&quot;</span> &lt;&lt; std::endl;</span><br><span class="line"><span class="comment">//首先C/C++中没有内置的二维数组，多维数组是都是指数组的数组，使用时很多情况下可以视为二维数组</span></span><br><span class="line"><span class="type">int</span> arr[<span class="number">5</span>][<span class="number">6</span>] = &#123;&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;&#125;; <span class="comment">//5行6列的数组，实际是一个含有5个元素的一维数组，每个元素又是一个含有6个int值的数组</span></span><br><span class="line"><span class="built_in">int</span> (*pp1)[<span class="number">6</span>] = arr;  <span class="comment">//pp1是一个指向含有6个int元素的数组的指针</span></span><br><span class="line"><span class="built_in">int</span> (*pp2)[<span class="number">5</span>][<span class="number">6</span>] = &amp;arr;  <span class="comment">//pp2是一个指向含有5行6列的二维数组的地址，即指向的是整个数组的数组</span></span><br><span class="line"></span><br><span class="line">   </span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;&amp;arr[0][0]: &quot;</span> &lt;&lt; &amp;arr[<span class="number">0</span>][<span class="number">0</span>] &lt;&lt; <span class="string">&quot;\tarr: &quot;</span> &lt;&lt; &amp;arr &lt;&lt; <span class="string">&quot;\tpp1: &quot;</span> &lt;&lt; pp1</span><br><span class="line">          &lt;&lt; <span class="string">&quot;\t*pp2: &quot;</span> &lt;&lt; *pp2 &lt;&lt; <span class="string">&quot;\t**arr: &quot;</span> &lt;&lt; **arr &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用指针访问二维数组</span></span><br><span class="line"><span class="keyword">for</span> (<span class="built_in">int</span> (*pr)[<span class="number">6</span>] = arr; pr != arr + <span class="number">5</span>; ++pr) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> *pc = *pr; pc != *pr + <span class="number">6</span>; ++pc)</span><br><span class="line">        std::cout &lt;&lt; *pc &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    std::cout &lt;&lt; std::endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>二维数组arr[i][j]会被解析成<em>(</em>(arr + i) + j)，arr依然是第一个元素的地址， 此处的第一个元素即可以表示arr[0][0]（即二维数组的第一个元素），也可以表示以行为元素的一维数组的 第一个元素<em>(arr + 0)（即第一行的6个元素），显然这两个地址是相同的。所以对arr增加1，即移到下一个 元素的地址，下一个元素即第二行。arr的元素是含有6个int的一维数组，所以指向arr的指针类型也必须是含有 6个int的一维数组，所以</em>pp1的括号和后面的[6]必须要有。 pp2的解释与p2类似</p>
<h1 id="函数和数组"><a href="#函数和数组" class="headerlink" title="函数和数组"></a>函数和数组</h1><p>首先明确C/C++中函数无法返回数组，所以对数组的操作和返回只能通过指针，而数组名本身就是个地址，可以看成是个常量指针<br>以下两种方式定义一个返回二维数组的函数，实际上返回的依然是指针  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用typedef定义一个类型别名，该类型为一个指针，该指针指向含有6个int元素的数组</span></span><br><span class="line"><span class="comment">//小括号不能省略，否则将成为含有6个指针的数组。由于二维数组就是数组的数组，所以该函数可以</span></span><br><span class="line"><span class="comment">//接受一个列数为6的二维数组作为参数，并返回一个指针，该指针同样指向含有6个int元素的数组  </span></span><br><span class="line"><span class="function"><span class="keyword">typedef</span> <span class="title">int</span> <span class="params">(*Tarr)</span>[6]</span>;</span><br><span class="line"><span class="function">Tarr <span class="title">func</span><span class="params">(<span class="type">int</span> (*arr)[<span class="number">6</span>])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述声明等价于</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">int</span> (*<span class="built_in">func</span>(<span class="built_in">int</span> (*arr)[<span class="number">6</span>]))[<span class="number">6</span>]</span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> c[<span class="number">5</span>][<span class="number">6</span>];</span><br><span class="line">Tarr b = <span class="built_in">func</span>(c);</span><br><span class="line">std::cout &lt;&lt; <span class="string">&quot;&amp;b[1][1]: &quot;</span> &lt;&lt; &amp;b[<span class="number">1</span>][<span class="number">1</span>] &lt;&lt; <span class="string">&quot;\t&amp;c[1][1]: &quot;</span> &lt;&lt; &amp;c[<span class="number">1</span>][<span class="number">1</span>] &lt;&lt; std::endl;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="函数和指针"><a href="#函数和指针" class="headerlink" title="函数和指针"></a>函数和指针</h1><p>这里的介绍非常详细<a target="_blank" rel="noopener" href="http://www.cnblogs.com/TenosDoIt/p/3164081.html">http://www.cnblogs.com/TenosDoIt/p/3164081.html</a><br>C语言函数指针的定义形式：<code>返回类型 (*函数指针名称)(参数类型1, 参数类型2, 参数类型3, ...);</code><br>C++语言函数指针的定义形式：<code>返回类型 (类名称::*函数指针名称)(参数类型1, 参数类型2, 参数类型3, ...);</code><br>注意，当定义指向类的成员函数的函数指针时，该成员函数如果不是静态成员函数，也必须通过类对象来调用。不能定义指向一个类对象的函数的函数指针。<br>与C++类相关的函数指针的定义都需要使用作用域运算符<code>::</code>，赋值都要带取地址符<code>&amp;</code>，通过类对象访问需要使用<code>.*</code>，通过类对象指针调用成员函数指针需要使<code>-&gt;*</code>  </p>
<h2 id="普通函数指针和类成员函数指针"><a href="#普通函数指针和类成员函数指针" class="headerlink" title="普通函数指针和类成员函数指针"></a>普通函数指针和类成员函数指针</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">tf</span><span class="params">(<span class="type">char</span> c, <span class="type">int</span> i)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;Normal: &quot;</span> &lt;&lt; c &lt;&lt; i &lt;&lt; std::endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">A</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">func1</span><span class="params">(<span class="type">char</span> c, <span class="type">int</span> i)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        std::cout &lt;&lt; <span class="string">&quot;Class func1: &quot;</span> &lt;&lt; c &lt;&lt; i &lt;&lt; std::endl;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">func2</span><span class="params">(<span class="type">char</span> c, <span class="type">int</span> i)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        std::cout &lt;&lt; <span class="string">&quot;Class func2: &quot;</span> &lt;&lt; c &lt;&lt; i &lt;&lt; std::endl;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">static</span> <span class="type">void</span> <span class="title">func3</span><span class="params">(<span class="type">char</span> c, <span class="type">int</span> i)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        std::cout &lt;&lt; <span class="string">&quot;Class func2: &quot;</span> &lt;&lt; c &lt;&lt; i &lt;&lt; std::endl;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">func4</span><span class="params">(<span class="type">char</span> c, <span class="type">int</span> i)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        std::cout &lt;&lt; <span class="string">&quot;Class func2: &quot;</span> &lt;&lt; c &lt;&lt; i &lt;&lt; std::endl;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">void</span> (*pf)(<span class="type">char</span>, <span class="type">int</span>) = <span class="literal">NULL</span>;   <span class="comment">//普通函数指针定义并使其指向NULL</span></span><br><span class="line">    pf = tf;</span><br><span class="line">    <span class="built_in">pf</span>(<span class="string">&#x27;t&#x27;</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">void</span> (A::*pf1)(<span class="type">char</span>, <span class="type">int</span>) = &amp;A::func1;  <span class="comment">//指向类的非静态成员函数的指针，初始时必须使用取地址符</span></span><br><span class="line">    A obj_a;</span><br><span class="line">    (obj_a.*pf1)(<span class="string">&#x27;f&#x27;</span>, <span class="number">1</span>);  <span class="comment">//这里必须要使用括号把前面对象和函数指针名括住，且必须使用.*，而不是-&gt;</span></span><br><span class="line"><span class="comment">//    pf = obj_a.func1;  //错误，不能让函数指针pf指向对象的指针，即使他们参数和返回值一样</span></span><br><span class="line"></span><br><span class="line">    A obj_b;</span><br><span class="line">    pf1 = &amp;A::func2;</span><br><span class="line">    (obj_b.*pf1)(<span class="string">&#x27;f&#x27;</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    pf = A::func3;  <span class="comment">//静态成员可以像普通函数一样不加取地址符</span></span><br><span class="line">    <span class="built_in">pf</span>(<span class="string">&#x27;f&#x27;</span>, <span class="number">3</span>);   <span class="comment">//因为是静态成员，所以不需要对象，可以直接通过函数指针调用</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">void</span> (A::*pfc)(<span class="type">char</span>, <span class="type">int</span>) <span class="type">const</span> = &amp;A::func4;  <span class="comment">//const 函数的函数指针也必须带有const</span></span><br><span class="line">    A obj_c;</span><br><span class="line">    (obj_c.*pfc)(<span class="string">&#x27;c&#x27;</span>, <span class="number">4</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出结果：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Normal: t1</span><br><span class="line">Class func1: f1</span><br><span class="line">Class func2: f1</span><br><span class="line">Class func2: f3</span><br><span class="line">Class func2: c4</span><br></pre></td></tr></table></figure>

<h2 id="函数指针作为参数和返回值"><a href="#函数指针作为参数和返回值" class="headerlink" title="函数指针作为参数和返回值"></a>函数指针作为参数和返回值</h2><p>要善用typedef简化函数指针类型的定义。具体看下面的一个例子：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">display</span><span class="params">(<span class="type">char</span> c, <span class="type">int</span> i)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> n = <span class="number">0</span>; n &lt; i; ++n)</span><br><span class="line">        std::cout &lt;&lt; c;</span><br><span class="line">    std::cout &lt;&lt; std::endl;</span><br><span class="line">    <span class="keyword">return</span> i;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//接受一个指向形如display的函数指针，一个char和一个int作为参数的函数</span></span><br><span class="line"><span class="comment">//没有返回值</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">dfun</span><span class="params">(<span class="type">int</span> (*pp)(<span class="type">char</span>, <span class="type">int</span>), <span class="type">char</span> c, <span class="type">int</span> i)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">pp</span>(c, i);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//该函数与dfun函数类似，只是其返回值为指向形如display函数的函数指针</span></span><br><span class="line"><span class="built_in">int</span> (*<span class="built_in">pdfun</span>(<span class="built_in">int</span> (*pd)(<span class="type">char</span>, <span class="type">int</span>), <span class="type">char</span> c, <span class="type">int</span> i))(<span class="type">char</span>, <span class="type">int</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">pd</span>(c, i);</span><br><span class="line">    <span class="keyword">return</span> pd;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">dfun</span>(display, <span class="string">&#x27;d&#x27;</span>, <span class="number">6</span>);  <span class="comment">//调用带有函数指针作为参数的函数</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">int</span> (*pd)(<span class="type">char</span>, <span class="type">int</span>) = <span class="built_in">pdfun</span>(display, <span class="string">&#x27;p&#x27;</span>, <span class="number">7</span>);  <span class="comment">//调用返回值为函数指针的函数</span></span><br><span class="line">    <span class="built_in">pd</span>(<span class="string">&#x27;p&#x27;</span>, <span class="number">8</span>);  <span class="comment">//调用函数指针的返回值的函数</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//使用typedef简化函数指针类型的定义</span></span><br><span class="line">    <span class="comment">//定义一个指向形如display的函数指针的类型pd_type</span></span><br><span class="line">    <span class="function"><span class="keyword">typedef</span> <span class="title">int</span> <span class="params">(*pd_type)</span><span class="params">(<span class="type">char</span>, <span class="type">int</span>)</span></span>;</span><br><span class="line">    pd_type pdd = display;</span><br><span class="line">    <span class="built_in">pdd</span>(<span class="string">&#x27;s&#x27;</span>, <span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//定义一个指向形如pdfun的函数指针的类型pdfun_tye类型</span></span><br><span class="line">    <span class="function"><span class="keyword">typedef</span> <span class="title">int</span> <span class="params">(*(*pdfun_type)(pd_type, <span class="type">char</span>, <span class="type">int</span>))</span><span class="params">(<span class="type">char</span>, <span class="type">int</span>)</span></span>;</span><br><span class="line">    pdfun_type ppdfun = pdfun;</span><br><span class="line">    pd_type ppd = <span class="built_in">ppdfun</span>(pdd, <span class="string">&#x27;u&#x27;</span>, <span class="number">12</span>);  <span class="comment">//调用并获取其返回的函数指针</span></span><br><span class="line">    <span class="built_in">ppd</span>(<span class="string">&#x27;x&#x27;</span>, <span class="number">15</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">dddddd</span><br><span class="line">ppppppp</span><br><span class="line">pppppppp</span><br><span class="line">ssssssssss</span><br><span class="line">uuuuuuuuuuuu</span><br><span class="line">xxxxxxxxxxxxxxx</span><br></pre></td></tr></table></figure>

<h2 id="函数指针数组"><a href="#函数指针数组" class="headerlink" title="函数指针数组"></a>函数指针数组</h2><p>举例说明：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">float</span> <span class="title">f1</span><span class="params">(<span class="type">float</span> a, <span class="type">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    std::cout &lt;&lt; a &lt;&lt; <span class="string">&quot; + &quot;</span> &lt;&lt; b &lt;&lt; <span class="string">&quot; = &quot;</span> &lt;&lt; a + b &lt;&lt; std::endl;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">float</span> <span class="title">f2</span><span class="params">(<span class="type">float</span> a, <span class="type">int</span> b)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    std::cout &lt;&lt; a &lt;&lt; <span class="string">&quot; * &quot;</span> &lt;&lt; b &lt;&lt; <span class="string">&quot; = &quot;</span> &lt;&lt; a * b &lt;&lt; std::endl;</span><br><span class="line">    <span class="keyword">return</span> a * b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">//定义并初始化函数指针数组</span></span><br><span class="line">    <span class="built_in">float</span> (*pfa[<span class="number">2</span>])(<span class="type">float</span>, <span class="type">int</span>) = &#123;f1, f2&#125;;</span><br><span class="line">    <span class="comment">//调用该函数指针数组</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">2</span>; ++i) &#123;</span><br><span class="line">        pfa[i](<span class="number">3</span>, <span class="number">9</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//使用typedef简化定义</span></span><br><span class="line">    <span class="function"><span class="keyword">typedef</span> <span class="title">float</span> <span class="params">(*f_type)</span><span class="params">(<span class="type">float</span>, <span class="type">int</span>)</span></span>;</span><br><span class="line">    f_type pfa2[] = &#123;f1, f2&#125;;</span><br><span class="line">    <span class="comment">//调用该函数指针数组</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">2</span>; ++i) &#123;</span><br><span class="line">        pfa2[i](<span class="number">6</span>, <span class="number">8</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">3 + 9 = 12</span><br><span class="line">3 * 9 = 27</span><br><span class="line">6 + 8 = 14</span><br><span class="line">6 * 8 = 48</span><br></pre></td></tr></table></figure>

    </div>

    
    
    
        

  <div class="followme">
    <p>Welcome to my other publishing channels</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/CPP/" rel="tag"># CPP</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2016/03/14/dynamic-two-dimension-arr/" rel="prev" title="C/C++中二维动态数组的使用方法举例">
      <i class="fa fa-chevron-left"></i> C/C++中二维动态数组的使用方法举例
    </a></div>
      <div class="post-nav-item">
    <a href="/2016/03/19/gdb/" rel="next" title="GDB调试详解">
      GDB调试详解 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E4%B8%8E%E6%8C%87%E9%92%88"><span class="nav-number">1.</span> <span class="nav-text">一维数组与指针</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E4%B8%8E%E6%8C%87%E9%92%88"><span class="nav-number">2.</span> <span class="nav-text">二维数组与指针</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E5%92%8C%E6%95%B0%E7%BB%84"><span class="nav-number">3.</span> <span class="nav-text">函数和数组</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E5%92%8C%E6%8C%87%E9%92%88"><span class="nav-number">4.</span> <span class="nav-text">函数和指针</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%99%AE%E9%80%9A%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E5%92%8C%E7%B1%BB%E6%88%90%E5%91%98%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88"><span class="nav-number">4.1.</span> <span class="nav-text">普通函数指针和类成员函数指针</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E5%92%8C%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="nav-number">4.2.</span> <span class="nav-text">函数指针作为参数和返回值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84"><span class="nav-number">4.3.</span> <span class="nav-text">函数指针数组</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="blueyi"
      src="/images/default_avatar.jpg">
  <p class="site-author-name" itemprop="name">blueyi</p>
  <div class="site-description" itemprop="description">心怀善意，虛怀若谷！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">104</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">68</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/blueyi" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;blueyi" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://maxwi.com/" title="http:&#x2F;&#x2F;maxwi.com" rel="noopener" target="_blank">Maxwi</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2014 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">blueyi</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="Symbols count total">750k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">11:22</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '0f8243eb2c8b2207980f',
      clientSecret: 'd159633a33519d3b7a48b0ca729032f7d1f38a41',
      repo        : 'notes',
      owner       : 'blueyi',
      admin       : ['blueyi'],
      id          : '75457fadefd71f1a503e920f3fd6a658',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

    </div>
</body>
</html>
